﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Labthink.PCSoft.UniversalHost4.UserControls.CustomControls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:Labthink.PCSoft.UniversalHost4.UserControls.CustomControls"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:Labthink.PCSoft.UniversalHost4.UserControls.CustomControls;assembly=Labthink.PCSoft.UniversalHost4.UserControls.CustomControls"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:IconToggleButton/>
    ///
    /// </summary>
    public class IconToggleButton : ToggleButton
    {
        public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register(nameof(Radius), typeof(CornerRadius), typeof(IconToggleButton), new PropertyMetadata(new CornerRadius(0), OnRadiusPropertyChanged));

        private static void OnRadiusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is IconToggleButton a)
            {
                a.RadiusChanged((CornerRadius)e.NewValue, (CornerRadius)e.OldValue);

            }
        }

        public CornerRadius Radius
        {
            get => (CornerRadius)GetValue(RadiusProperty);
            set => SetValue(RadiusProperty, value);
        }

        public static readonly DependencyProperty RadiusLeftProperty = DependencyProperty.Register(nameof(RadiusLeft), typeof(CornerRadius), typeof(IconToggleButton), new PropertyMetadata(new CornerRadius(0)));

        public static readonly DependencyProperty RadiusRightProperty = DependencyProperty.Register(nameof(RadiusRight), typeof(CornerRadius), typeof(IconToggleButton), new PropertyMetadata(new CornerRadius(0), OnRadiusPropertyChanged));

        public CornerRadius RadiusLeft
        {
            get => (CornerRadius)GetValue(RadiusLeftProperty);
            set => SetValue(RadiusLeftProperty, value);
        }

        public CornerRadius RadiusRight
        {
            get => (CornerRadius)GetValue(RadiusRightProperty);
            set => SetValue(RadiusRightProperty, value);
        }

        public static readonly DependencyProperty SelectedBackgroundProperty = DependencyProperty.Register(
            "SelectedBackground", typeof(Brush), typeof(IconToggleButton), new PropertyMetadata(default(Brush)));

        public Brush SelectedBackground
        {
            get => (Brush) GetValue(SelectedBackgroundProperty);
            set => SetValue(SelectedBackgroundProperty, value);
        }

        public static readonly DependencyProperty SelectedIconProperty = DependencyProperty.Register(
            "SelectedIcon", typeof(string), typeof(IconToggleButton), new PropertyMetadata(default(string)));

        public string SelectedIcon
        {
            get => (string) GetValue(SelectedIconProperty);
            set => SetValue(SelectedIconProperty, value);
        }

        public static readonly DependencyProperty IconSizeProperty = DependencyProperty.Register(
            "IconSize", typeof(double), typeof(IconToggleButton), new PropertyMetadata(default(double)));

        public double IconSize
        {
            get => (double) GetValue(IconSizeProperty);
            set => SetValue(IconSizeProperty, value);
        }

        public static readonly DependencyProperty SelectedForegroundProperty = DependencyProperty.Register(
            "SelectedForeground", typeof(Brush), typeof(IconToggleButton), new PropertyMetadata(default(Brush)));

        public Brush SelectedForeground
        {
            get => (Brush)GetValue(SelectedForegroundProperty);
            set => SetValue(SelectedForegroundProperty, value);
        }

        public static readonly DependencyProperty SelectedIconForegroundProperty = DependencyProperty.Register(
            "SelectedIconForeground", typeof(Brush), typeof(IconToggleButton), new PropertyMetadata(default(Brush)));

        public Brush SelectedIconForeground
        {
            get => (Brush) GetValue(SelectedIconForegroundProperty);
            set => SetValue(SelectedIconForegroundProperty, value);
        }

        public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register(
            "SelectedText", typeof(string), typeof(IconToggleButton), new PropertyMetadata(default(string)));

        public string SelectedText
        {
            get => (string) GetValue(SelectedTextProperty);
            set => SetValue(SelectedTextProperty, value);
        }

        public static readonly DependencyProperty UnSelectedIconProperty = DependencyProperty.Register(
            "UnSelectedIcon", typeof(string), typeof(IconToggleButton), new PropertyMetadata(default(string)));

        public string UnSelectedIcon
        {
            get => (string) GetValue(UnSelectedIconProperty);
            set => SetValue(UnSelectedIconProperty, value);
        }

        public static readonly DependencyProperty UnSelectedForegroundProperty = DependencyProperty.Register(
            "UnSelectedForeground", typeof(Brush), typeof(IconToggleButton), new PropertyMetadata(default(Brush)));

        public Brush UnSelectedForeground
        {
            get => (Brush)GetValue(UnSelectedForegroundProperty);
            set => SetValue(UnSelectedForegroundProperty, value);
        }

        public static readonly DependencyProperty UnSelectedIconForegroundProperty = DependencyProperty.Register(
            "UnSelectedIconForeground", typeof(Brush), typeof(IconToggleButton), new PropertyMetadata(default(Brush)));

        public Brush UnSelectedIconForeground
        {
            get => (Brush) GetValue(UnSelectedIconForegroundProperty);
            set => SetValue(UnSelectedIconForegroundProperty, value);
        }

        public static readonly DependencyProperty UnSelectedTextProperty = DependencyProperty.Register(
            "UnSelectedText", typeof(string), typeof(IconToggleButton), new PropertyMetadata(default(string)));

        public string UnSelectedText
        {
            get => (string) GetValue(UnSelectedTextProperty);
            set => SetValue(UnSelectedTextProperty, value);
        }
        
        static IconToggleButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(IconToggleButton), new FrameworkPropertyMetadata(typeof(IconToggleButton)));
        }

        
        public void RadiusChanged(CornerRadius newValue, CornerRadius oldValue)
        {
            if (newValue.Equals(oldValue)) return;
          
            RadiusRight = new CornerRadius(newValue.TopLeft, 0, 0, newValue.BottomLeft);
            RadiusLeft = new CornerRadius(0, newValue.TopRight, newValue.BottomRight, 0);
        }

    }
}
